<template>
	<view class="page-content">
		<!-- 选择器背景 -->
		<view class="page-mask"></view>
		<!-- 选择器主体 -->
		<view class="page-main" :style="'height:60%'">
			<!-- 操作区域 包含取消和确定 -->
			<view class="main-operate">
				<text class="title-txt" @click.stop="handleCancel">{{$t('uni-address-selector.UniAddressSelector.Cancel')}}</text>
				<text class="title-txt" @click.stop="handleSubmit">{{$t('uni-address-selector.UniAddressSelector.Sure')}}</text>
			</view>
			<!-- 已选择的地址信息 -->
			<view class="main-select">
				<text v-if="showIndex == 0 || provinceObj.name"
					:class="showIndex == 0 ? 'select' : ''">{{ provinceObj.name || $t('uni-address-selector.UniAddressSelector.Please') }}</text>
				<text v-if="showIndex == 1 || cityObj.name"
					:class="showIndex == 1 ? 'select' : ''">{{ cityObj.name || $t('uni-address-selector.UniAddressSelector.Please') }}</text>
				<text v-if="showIndex == 2 || areaObj.name"
					:class="showIndex == 2 ? 'select' : ''">{{ areaObj.name || $t('uni-address-selector.UniAddressSelector.Please') }}</text>
				<text v-if="(showIndex == 3 || streetObj.name)"
					:class="showIndex == 3 ? 'select' : ''">{{ streetObj.name || $t('uni-address-selector.UniAddressSelector.Please') }}</text>
			</view>
			<!-- 待选择的地址信息 -->
			<view class="main-list">
				<view v-if="showIndex == 0">
					<view class="list-box" :class="provinceObj.code == item.code ? 'active' : ''"
						@click="handleSelectAddress(0, item)" v-for="item in provinceData" :key="'province'+item.code">
						<text>{{ item.name }}</text>
					</view>
				</view>
				<view v-if="showIndex == 1">
					<view class="list-box" :class="cityObj.code == item.code ? 'active' : ''"
						@click="handleSelectAddress(1, item)" v-for="item in cityData" :key="'city'+item.code">
						<text>{{ item.name }}</text>
					</view>
				</view>
				<view v-if="showIndex == 2">
					<view class="list-box" :class="areaObj.code == item.code ? 'active' : ''"
						@click="handleSelectAddress(2, item)" v-for="item in areaData" :key="'area'+item.code">
						<text>{{ item.name }}</text>
					</view>
				</view>
				<view v-if="showIndex == 3">
					<view class="list-box" :class="streetObj.code+streetObj.town == item.code+item.town ? 'active' : ''"
						@click="handleSelectAddress(3, item)" v-for="item in streetsData" :key="item.code+item.town">
						<text>{{ item.name }}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		provinces,
		city,
		area,
		town
	} from "./province-city-china.js"
	export default {
		name: "UniAddressSelector",
		props: {
			areaInfoSelected: {},
		},
		data() {
			return {
				showIndex: 0, //地区显示
				provinceObj: {},
				cityObj: {},
				areaObj: {},
				streetObj: {},
				heightCot: 30, //设置屏幕高度 0 ~ 100
				provinceData: [], // 当前展示的省数据
				cityData: [], // 当前展示的市数据
				areaData: [], //当前展示的区数据
				streetsData: [], //当前展示的区数据
				areaInfoObj: {},
			};
		},
		mounted() {
			this.provinceData = provinces
			this.cityData = city
			this.areaData = area
			this.streetsData = town
			// if (this.areaInfoSelected.fullAreaTextInitial) {
			// 	let {
			// 		fullAreaTextInitial
			// 	} = this.areaInfoSelected;
			// 	let _arr = fullAreaTextInitial.split(",");

			// 	this.provinceObj = this.areaInfoSelected.provinceObj;
			// 	this.cityObj = this.areaInfoSelected.cityObj;
			// 	this.areaObj = this.areaInfoSelected.areaObj;
			// 	this.streetObj = this.areaInfoSelected.streetObj;
			// }
			this.getScreen();
		},
		onShow() {},
		methods: {
			//组件高度自适应
			getScreen() {
				let that = this;
				uni.getSystemInfo({
					success: (res) => {
						that.heightCot = (res.safeArea.height * 2) / 2;
					},
				});
			},
			//取消
			handleCancel() {
				this.$emit("cancel");
			},
			//确定
			handleSubmit() {
				this.areaInfoObj = {
					province: this.provinceObj.name,
					city: this.cityObj.name,
					area: this.areaObj.name,
					street: this.streetObj.name
				}
				// console.log(this.areaInfoObj);
				this.$emit("confirm", this.areaInfoObj);
			},

			handleSelectAddress(type, obj) {
				switch (type) {
					case 0:
						//选择省
						this.provinceObj = obj;
						this.showIndex = 1;
						if (obj.province == "11" || obj.province == "12" || obj.province == "50" || obj.province == "31") {
							this.showIndex = 2;
						}
						this.cityData = this.cityData.filter(item => {
							return item.province == obj.province
						});
						this.areaData = this.areaData.filter(item => {
							return item.province == obj.province
						});
						this.streetsData = this.streetsData.filter(item => {
							return item.province == obj.province
						});
						break;
					case 1:
						//选择市
						this.cityObj = obj;
						this.showIndex = 2;
						this.areaData = this.areaData.filter(item => {
							return item.city == obj.city
						});
						this.streetsData = this.streetsData.filter(item => {
							return item.city == obj.city
						});
						break;
					case 2:
						//选择区
						this.areaObj = obj;
						this.showIndex = 3;
						this.streetsData = this.streetsData.filter(item => {
							return item.area == obj.area
						});
						break;
					case 3:
						//选择街道
						this.streetObj = obj;
						break;
					default:
						break;
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	.page-mask {
		position: fixed;
		width: 100%;
		height: 100vh;
		z-index: 998;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: rgba(0, 0, 0, 0.6);
	}

	.page-main {
		height: 100vh;
		position: fixed;
		z-index: 999;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #ffffff;
		padding: 20rpx;
		border-top: 1rpx solid rgba(0, 0, 0, 0.2);
		border-radius: 16rpx 16rpx 0 0;
		max-height: 60%;

		.main-operate {
			padding: 0 10rpx 10rpx 10rpx;
			display: flex;
			justify-content: space-between;

			.title-txt {
				color: #343434;
				font-size: 30rpx;

				&:nth-child(2) {
					color: #ff4242;
				}
			}
		}

		.main-select {
			display: flex;
			justify-content: flex-start;
			color: #343434;
			font-size: 30rpx;
			height: 100rpx;
			line-height: 100rpx;

			text {
				display: inline-block;
				width: 25%;
				text-align: center;
				overflow: hidden;
				-webkit-line-clamp: 1;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				text-align: center;
			}

			.select {
				color: #ff4242;
				position: relative;

				&::after {
					content: "";
					width: 100%;
					height: 6rpx;
					background: #ff4242;
					position: absolute;
					left: 0;
					bottom: -8rpx;
				}
			}
		}

		.main-list {
			overflow-y: auto;
			color: #343434;
			font-size: 30rpx;
			width: 100%;
			height: 72%;
			overflow: auto;

			.list-box {
				display: flex;
				flex-direction: column;
				height: 100rpx;
				line-height: 100rpx;
				border-bottom: 2rpx solid #dedede;
			}

			.list-box:last-child {
				border-bottom: none;
			}

			.active {
				color: #ff4242;
			}
		}
	}
</style>
